﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using KM.BUS;
using KM.ENT;
using KM.Models;
using CORE;

namespace KM.Areas.Admin.Controllers
{
    public class SanPhamController : Controller
    {
        int pageSize = 20;
        //
        // GET: /SanPham/
        public ActionResult DanhSachSlide()
        {
            //lay danh sach loai san pham
            busSanPham busSP = new busSanPham();
            List<entSanPham> lst = new List<entSanPham>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            lst = busSP.getSlide();
            return View(lst);
        }
        [ReadOnlyLogOn]
        public ActionResult dsSanPham(int Id = 1)
        {
            //lay danh sach loai san pham
            binLSP(Id.ToString());
            ViewBag.me = Id;
            busSanPham busSP = new busSanPham();
            List<entSanPham> lst = new List<entSanPham>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            lst = busSP.getAll(Id, ref page);
            ViewBag.page = page;
            return View(lst);
        }
        void binLSP(string check)
        {
            //lay danh sach loai san pham
            busLoaiSanPham busLSP = new busLoaiSanPham();
            List<entLoaiSanPham> lst = new List<entLoaiSanPham>();
            lst = busLSP.getAll();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in lst)
            {
                items.Add(new SelectListItem()
                {
                    Value = item.Id.ToString(),
                    Text = item.TenNhomSanPham,
                    Selected = (item.Id.ToString() == check ? true : false)
                });
            }
            ViewBag.lstLSP = items;
        }
        [HttpPost]

        public ActionResult dsSanPham(FormCollection collection)
        {
            //lay danh sach loai san pham
            binLSP(collection["LSP"]);
            busSanPham busSP = new busSanPham();
            List<entSanPham> lst = new List<entSanPham>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            if (collection["LSP"].ToString() != "")
            {
                int Id = int.Parse(collection["LSP"].ToString());
                lst = busSP.getAll(Id, ref page);
                ViewBag.me = Id;
            }
            ViewBag.page = page;
            return View(lst);
        }
        [ReadOnlyLogOn]
        public ActionResult edit(int Id = 0)
        {
            busLoaiSanPham busLKM = new busLoaiSanPham();
            List<entLoaiSanPham> lst2 = new List<entLoaiSanPham>();
            lst2 = busLKM.getAll();
            ViewBag.lstLKM = lst2;

            busSanPham busSP = new busSanPham();
            entSanPham entSP = new entSanPham();
            entSP = busSP.getInfo(Id);
            return View(entSP);
        }
        public ActionResult delete(int Id)
        {
            busSanPham busSP = new busSanPham();
            busSP.delete(Id);
            return RedirectToAction("dsSanPham", null, new {Id=1 });
        }
        [HttpPost]
        [ReadOnlyLogOn]
        [ValidateInput(false)]
        public ActionResult edit(entSanPham entSP)
        {
            if (!ModelState.IsValid)
            {
                busLoaiSanPham busLKM = new busLoaiSanPham();
                List<entLoaiSanPham> lst2 = new List<entLoaiSanPham>();
                lst2 = busLKM.getAll();
                ViewBag.lstLKM = lst2;
                
                return View(entSP);
            }


            HttpPostedFileBase photo = Request.Files["Image"];
            String filename = DateTime.Now.ToString("ddmmyyhhmmss").ToString();
            if (photo != null)
            {
                if (photo.ContentLength > 0)
                {
                    photo.SaveAs(Server.MapPath("/images/") + filename + ".jpg");
                    entSP.Image = filename + ".jpg";
                }
            }

            HttpPostedFileBase photoSlide = Request.Files["Image1"];
            if (photoSlide != null)
            {
                if (photoSlide.ContentLength > 0)
                {
                    photoSlide.SaveAs(Server.MapPath("/images/") + filename + "1.jpg");
                    entSP.Image1 = filename + "1.jpg";
                }
            }

            busSanPham busSP = new busSanPham();
            busSP.createEdit(entSP);
            return RedirectToAction("edit", new { Id = entSP.Id });


        }
        [HttpPost]
        public ActionResult DeleteSlide(FormCollection collection)
        {
            int Id = int.Parse(collection["Id"].ToString());
            busSanPham busSP = new busSanPham();
            busSP.deleteSlide(Id);
            ViewBag.me = "true";
            return View();
        }
        public ActionResult DeleteSlide(int Id)
        {
            busSanPham busSP = new busSanPham();
            busSP.deleteSlide(Id);
            return RedirectToAction("DanhSachSlide");
        }
    }
}
